package xhwl.insurance.ui;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import cn.jpush.android.api.JPushInterface;
import xhwl.android.common.log.XHLog;
import xhwl.android.common.util.SPUtil;
import xhwl.android.common.util.StringUtil;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.util.AppUtil;
import xhwl.insurance.util.SpUtil;
import xhwl.retrofitrx.token.LoadTokenCallback;
import xhwl.retrofitrx.token.TokenUtil;

public class SplashActivity extends BaseActivity {

    private static final String TAG = "SplashActivity";
    private String memberId;
    private RelativeLayout splash;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题  
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        memberId = SPUtil.getPersonInfoSp().getString(SPUtil.SPPersonInfo.MEMBER_ID, "");
        if (!SpUtil.getString(SpUtil.GUIDE, "").equals("2")) {
            redirectToGuide();
        } else {
            setContentView(R.layout.activity_splash);
            splash = (RelativeLayout) findViewById(R.id.splash);
            new MyThread().start();
        }

    }

    private void redirectToGuide() {
        Intent intent = new Intent(this, AppGuideActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onResume() {
        super.onResume();
        JPushInterface.onResume(this);// jpush统计代码
        XHLog.e("jpush id=" + SPUtil.getCommon().getString(SPUtil.Common.JPUSH_ID, ""));
    }

    @Override
    public void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    class MyThread extends Thread {
        @Override
        public void run() {
            try {
                sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            TokenUtil.initToken(SplashActivity.this, false, new LoadTokenCallback() {
                @Override
                public void onLoadSuccess(String token) {
                    if (!StringUtil.isEmpty(memberId)) {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class)
                                .putExtra("BACK_STATE","0"));
                    } else {
                        startActivity(new Intent(SplashActivity.this, LoginActivityV2.class));
                    }
                    finish();
                }

                @Override
                public void onLoadFail(String msg) {
                    finish();
                }
            });

        }

        ;

    }

    @Override
    public void initViews() {
        // TODO Auto-generated method stub
    }

    @Override
    public void initListener() {
        // TODO Auto-generated method stub

    }

    @Override
    public void loadData() {

    }

}
